# -*- coding: utf-8 -*-
'''
@File: models.py
@Time: 2023/5/5 23:35
@Author: sunda
'''
'''
数据模型
'''
"""登录需要的数据库模型（用户表）"""
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import db


class User(UserMixin, db.Model):
    # 第一个参数指定字段类型，后面设置属性
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(128), nullable=False)
    password = db.Column(db.String(128), nullable=False)

    def __init__(self, username, password):
        self.username = username
        self.password = password

    def set_password(self, password):
        '''
        注册用户时 数据库密码加密展示
        :param password:
        :return:
        '''
        self.password = generate_password_hash(password)

    def check_password(self, password):
        '''
        登录密码解密
        :param password:
        :return:
        '''
        return check_password_hash(self.password, password)

    @property
    def is_active(self):
        return True

    @property
    def is_authenticated(self):
        return True

    @property
    def is_anonymous(self):
        return True

    # def get_id(self):
    #     try:
    #         return text_type(self.username)
    #     except AttributeError:
    #         raise NotImplementedError('No "id" attribute - override "get_id"')

''' 节假日表 '''
class Holiday(db.Model):
    __tablename__ = 'holidays'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    year = db.Column(db.Integer, primary_key=True, nullable=False)
    date = db.Column(db.String(50), nullable=False)
    area = db.Column(db.String(50), primary_key=True,nullable=False)
    note = db.Column(db.String(50), nullable=False)

    def __init__(self,year,date,area,note):
        self.year=year
        self.date=date
        self.area=area
        self.note= note

    # def __repr__(self):
    #     return f"<holidays year='{self.year}' date='{self.date}' area='{self.area}' note='{self.note}'"

''' 数据字典主表 '''
class DictType(db.Model):
    __tablename__ = 'dict_type'
    type_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    key = db.Column(db.Integer, primary_key=True, nullable=False)
    value = db.Column(db.String(50), nullable=False)
    desc = db.Column(db.String(50), primary_key=True,nullable=False)
    status = db.Column(db.String(50), nullable=False)

    def __init__(self,type_id,key,value,desc,status):
        self.type_id=type_id
        self.key=key
        self.value=value
        self.desc=desc
        self.status= status

''' 数据字典从表 '''
class DictItem(db.Model):
    __tablename__ = 'dict_item'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    type_id = db.Column(db.Integer, primary_key=True, nullable=False)
    sort_id = db.Column(db.String(50), nullable=False)
    key = db.Column(db.String(50), primary_key=True, nullable=False)
    value = db.Column(db.String(50), primary_key=True, nullable=False)
    desc = db.Column(db.String(50), primary_key=True,nullable=False)
    status = db.Column(db.String(50), nullable=False)

    def __init__(self,id,type_id,sort_id,key,value,desc,status):
        self.id=id
        self.type_id=type_id
        self.sort_id=sort_id
        self.key = key
        self.value = value
        self.desc=desc
        self.status= status

if __name__ == '__main__':
    user = User(username='admin', password=123456)
    db.session.add(user)
    db.session.commit()
